package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.data.network.task.GoodsOrderTask;
import com.zw.yzk.kabu.model.GoodsOrderModel;
import com.zw.yzk.kabu.utils.ResUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/2/8.
 */

public class GoodsOrderAdapter extends RecyclerView.Adapter<GoodsOrderAdapter.Holder> {

    private List<GoodsOrderModel> dataList;
    private Context context;

    public GoodsOrderAdapter(List<GoodsOrderModel> list) {
        dataList = new ArrayList<>();
        if (list != null) {
            dataList.addAll(list);
        }
    }

    public void refresh(List<GoodsOrderModel> list) {
        dataList.clear();
        if (list != null) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void add(List<GoodsOrderModel> list) {
        if (list != null) {
            int position = dataList.size();
            dataList.addAll(list);
            notifyItemRangeInserted(position, list.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_goods_order_shop_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final GoodsOrderModel model = dataList.get(position);
        holder.shopName.setText(model.getShopName());
        holder.orderState.setText(model.getOrderTypeStr());
        holder.totalPrice.setText(ResUtils.formatCurrency(context, model.getTotalPrice()));
        holder.goodsCount.setText(context.getResources().getString(R.string.order_goods_count, model.getGoodsCount()));
        String freight = "(含运费" + ResUtils.formatCurrency(context, model.getFreight()) + ")";
        holder.freight.setText(freight);
        holder.cancelTime.setText(model.getRefundTime());
        switch (model.getRefundState()) {
            case GoodsOrderTask.REFUND_STATE_REFUNDING:
                holder.cancelState.setText("退货中");
                break;
            case GoodsOrderTask.REFUND_STATE_SUCCESS:
                holder.cancelState.setText("退货成功");
                break;
            case GoodsOrderTask.REFUND_STATE_FAILED:
                holder.cancelState.setText("退货失败");
                break;
            default:
                break;
        }

        hideAll(holder);
        switch (model.getOrderType()) {
            case GoodsOrderTask.GOODS_ORDER_COMPLETE:
                holder.complete.setVisibility(View.VISIBLE);
                break;
            case GoodsOrderTask.GOODS_ORDER_UNDELIVER:
                holder.pendingDeliver.setVisibility(View.VISIBLE);
                break;
            case GoodsOrderTask.GOODS_ORDER_UNPAIED:
                holder.pendingPayment.setVisibility(View.VISIBLE);
                break;
            case GoodsOrderTask.GOODS_ORDER_UNRECEIVE:
                holder.pendingReceive.setVisibility(View.VISIBLE);
                break;
            case GoodsOrderTask.GOODS_ORDER_BACK:
                holder.cancel.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }

        //付款
        holder.pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null) {
                    listener.pay(model);
                }
            }
        });
        //催促发货
        holder.urge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null) {
                    listener.urge(model);
                }
            }
        });
        //查看物流
        holder.pViewLogistics.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.viewLogistics(model);
                }
            }
        });
        //查看物流
        holder.cViewLogistics.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.viewLogistics(model);
                }
            }
        });
        //确认收货
        holder.confirmReceive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null) {
                    listener.confirmReceive(model);
                }
            }
        });
        //申请退货
        holder.applyCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null) {
                    listener.applyCancel(model);
                }
            }
        });

        //查看详情
        if (model.getOrderType() != GoodsOrderTask.GOODS_ORDER_BACK) {
            holder.goodsList.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.viewDetails(model);
                    }
                }
            });
        }
        //设置商品列表
        holder.setAdapter(context, model, listener);
    }

    private void hideAll(Holder holder) {
        holder.pendingPayment.setVisibility(View.GONE);
        holder.pendingDeliver.setVisibility(View.GONE);
        holder.pendingReceive.setVisibility(View.GONE);
        holder.complete.setVisibility(View.GONE);
        holder.cancel.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.shop_name)
        TextView shopName;
        @Bind(R.id.order_state)
        TextView orderState;
        @Bind(R.id.goods_count)
        TextView goodsCount;
        @Bind(R.id.total_price)
        TextView totalPrice;
        @Bind(R.id.goods_list)
        RecyclerView goodsList;
        @Bind(R.id.layout_pending_payment)
        View pendingPayment;
        @Bind(R.id.pay)
        View pay;
        @Bind(R.id.layout_pending_deliver)
        View pendingDeliver;
        @Bind(R.id.urge)
        View urge;
        @Bind(R.id.layout_pending_receive)
        View pendingReceive;
        @Bind(R.id.view_logistics_pending)
        View pViewLogistics;
        @Bind(R.id.confirm_receive)
        View confirmReceive;
        @Bind(R.id.layout_complete)
        View complete;
        @Bind(R.id.view_logistics_complete)
        View cViewLogistics;
        @Bind(R.id.apply_cancel)
        View applyCancel;
        @Bind(R.id.layout_cancel_order)
        View cancel;
        @Bind(R.id.action_time)
        TextView cancelTime;
        @Bind(R.id.cancel_order_state)
        TextView cancelState;
        @Bind(R.id.freight)
        TextView freight;

        private GoodsOrderGoodsAdapter adapter;

        public Holder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);
        }

        public void setAdapter(Context context, final GoodsOrderModel model, final OnOrderItemClickedListener listener) {
            goodsList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
            if (adapter == null) {
                adapter = new GoodsOrderGoodsAdapter(model.getGoodsList());
                adapter.setOnItemClickedListener(new GoodsOrderGoodsAdapter.OnItemClickedListener() {
                    @Override
                    public void onClick() {
                        if (listener != null) {
                            listener.viewDetails(model);
                        }
                    }
                });
                goodsList.setAdapter(adapter);
            } else {
                adapter.refresh(model.getGoodsList());
            }
        }
    }

    public interface OnOrderItemClickedListener {
        void pay(GoodsOrderModel model);

        void urge(GoodsOrderModel model);

        void viewLogistics(GoodsOrderModel model);

        void confirmReceive(GoodsOrderModel model);

        void applyCancel(GoodsOrderModel model);

        void viewDetails(GoodsOrderModel model);
    }

    private OnOrderItemClickedListener listener;

    public void setOnOrderItemClickedListener(OnOrderItemClickedListener listener) {
        this.listener = listener;
    }
}
